package borge.web.teamspirit.client.widgets;

import java.util.ArrayList;
import java.util.List;

import borge.web.teamspirit.client.TeamSpirit;
import borge.web.teamspirit.client.TeamSpiritConstants;
import borge.web.teamspirit.client.data.IndepSerie;
import borge.web.teamspirit.client.data.Match;
import borge.web.teamspirit.client.data.SerieEnums;
import borge.web.teamspirit.client.interfaces.WidgetEventHandler;

import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Display a certain serie
 * @author andreas
 *
 */
public class ShowSerieWidget extends ShowTabbedDataWidget {

    private IndepSerie iSerie;
    private final String[] iTabTitles = {
            TeamSpirit.getConstants().general(),
            TeamSpirit.getConstants().matches(),
            TeamSpirit.getConstants().statistics(),
            TeamSpirit.getConstants().discussion()};
    
    public ShowSerieWidget(IndepSerie aSerie, WidgetEventHandler aEventHandler){
        
        super(aEventHandler);
        iSerie = aSerie;
  
        iPanel.setAnimationEnabled(true);
        
        for(int i = 0; i < iTabTitles.length; ++i){
            
            
        }
        
        int index = 0;
        iPanel.add(createGeneralPanel(), iTabTitles[index++]);
        iPanel.add(createMatchesPanel(), iTabTitles[index++]);
        iPanel.add(createStatisticsPanel(), iTabTitles[index++]);
        iPanel.add(createDiscussionsPanel(), iTabTitles[index++]);
        
        iPanel.selectTab(0);
        initWidget(iPanel);
        
    }
    
    private VerticalPanel createGeneralPanel(){
        
        VerticalPanel vp = new VerticalPanel();
        
        // List some general info about the serie in a table
        FlexTable table = new FlexTable();
        
        int row = 0;
        int col = 0;
        TeamSpiritConstants lcs = TeamSpirit.getConstants();
        
        table.setText(row, col++, lcs.type());
        table.setText(row, col++, SerieEnums.getLocalizedType(iSerie.getType()));
        
        row++;
        col = 0;
        table.setText(row, col++, lcs.season());
        table.setText(row, col++, iSerie.getSeason());

        row++;
        col = 0;
        table.setText(row, col++, lcs.division());
        table.setText(row, col++, iSerie.getDivision());
        
        row++;
        col = 0;
        table.setText(row, col++, lcs.start_date());
        table.setText(row, col++, "startdate");
        
        row++;
        col = 0;
        table.setText(row, col++, lcs.end_date());
        table.setText(row, col++, "enddate");
        
        row++;
        col = 0;
        table.setText(row, col++, lcs.status());
        table.setText(row, col++, SerieEnums.getLocalizedStatus(iSerie.getStatus()));
        
        if(SerieEnums.getStatusFromString(iSerie.getStatus()) == SerieEnums.Status.ENDED){
            row++;
            col = 0;
        	table.setText(row, col++, lcs.placement());
        	table.setText(row, col++, iSerie.getPlacement().toString());
        }
        
        
        vp.add(table);
        
        return vp;
    }
    
    private VerticalPanel createMatchesPanel(){
        VerticalPanel vp = new VerticalPanel();
        
        // Here we should actually embed ListMatchesWidget
        List<Match> matches = new ArrayList<Match>();
        ListMatchesWidget widget = new ListMatchesWidget(getWidgetEventHandler(), matches);
        vp.add(widget);
        
        return vp;
    }
    
    private VerticalPanel createStatisticsPanel(){
        VerticalPanel vp = new VerticalPanel();
        
        // won games, draw, lost, goals made, goals let in
        // skytteliga, attendance, 
        
        // We will probably have a generic statistics widget as well
        
        return vp;
    }
    
    private VerticalPanel createDiscussionsPanel(){
        VerticalPanel vp = new VerticalPanel();
        
        // Embed generic discussions panel
        
        return vp;
    }

}

